//
//  NewsDetailViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "TAMacro.h"

#import "AFNetworking.h"
#import "NewsViewController.h"

#import "Newsesarray.h"

#import "CustomWebview.h"
#import "NewsCustomCollectionViewCell.h"
#import "CustomView.h"
#import "BottomView.h"
#import "BottomDetailView.h"
#import "CollectionButtonViewController.h"

#import "UMSocialSnsService.h"
#import "UMSocialSnsPlatformManager.h"
@interface NewsDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NewsViewController *newsViewController;
//集合视图属性

@property (nonatomic,strong) CustomWebview *webView;
@property (nonatomic,strong) UICollectionView *newsDetailCollectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flow;

@property (nonatomic,strong) NSMutableArray<Newsesarray *> *newsDetailArray;

@property (nonatomic,strong) BottomView *bottomView;
@property (nonatomic,strong) BottomDetailView *bottomDetailView;
@property (nonatomic,strong) UIView *grayView;
@end

@implementation NewsDetailViewController

#pragma mark ---视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.tabBarController.tabBar.hidden = YES;
    [self creatCollectionView];
    [self createNavigationitem];
//    [self createbottomView];
    [self createGrayView];
    [self createBottomDetailView];
}

    
    
    
    
   

    


- (void)createGrayView
{
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _grayView.backgroundColor = [UIColor colorWithWhite:0.664 alpha:1.000];
    
}

- (void)createbottomView
{
    _bottomView = [[BottomView alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT - 200, SCREEN_WIDTH - 20, 49)];
    _bottomView.backgroundColor = [UIColor colorWithWhite:0.857 alpha:1.000];
    [self.view addSubview:_bottomView];
    [_bottomView.returnButton addTarget:self action:@selector(returnButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [_bottomView.leftButton addTarget:self action:@selector(leftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView.rightButton addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [_bottomView.centerButton addTarget:self action:@selector(centerButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)createBottomDetailView
{
    _bottomDetailView = [[BottomDetailView alloc] initWithFrame:CGRectMake(10, 300, SCREEN_WIDTH - 20, 200)];
    _bottomDetailView.backgroundColor = [UIColor whiteColor];
    [_bottomDetailView.collectionButton addTarget:self action:@selector(collectionButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}


- (void)createNavigationitem
{
    

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TitleLogo"]];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ButtonEntryToolClose"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemClicked)];
}
//返回按钮点击方法
- (void)leftBarButtonItemClicked
{
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)collectionButtonClicked
{
    CollectionButtonViewController *buttonVC = [CollectionButtonViewController new];

    buttonVC.guid = _newsDetailArray[_rowValue].guid;
    [self.navigationController pushViewController:buttonVC animated:NO];
}







#pragma mark ----中心图片的点击方法
- (void)centerButtonClicked
{
    _webView.hidden = YES;
    [self.view addSubview:_grayView];
    [self.view addSubview:_bottomDetailView];


}

#pragma mark ------返回按钮的点击方法
- (void)returnButtonClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -----左按钮点击方法
- (void)leftButtonClicked
{
    _newsDetailCollectionView.contentOffset = CGPointMake((_rowValue - 1) * SCREEN_WIDTH, 0);
    _rowValue--;
}
#pragma mark -----右按钮点击方法
- (void)rightButtonClicked
{
    _newsDetailCollectionView.contentOffset = CGPointMake((_rowValue + 1) * SCREEN_WIDTH, 0);
    _rowValue++;
}

#pragma mark ------创建collectionView
- (void)creatCollectionView
{
    self.navigationController.navigationBar.hidden = NO;
    _flow = [UICollectionViewFlowLayout new];
    _flow.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49);
    [_flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [_flow setMinimumInteritemSpacing:0];
    [_flow setMinimumLineSpacing:0];
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, 0);
    [_flow setSectionInset:inset];
    
    
    _newsDetailCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:_flow];

    
    [self.newsDetailCollectionView reloadData];
    [self newsDetailNetworkGet];
    
    CGPoint point = CGPointMake(_rowValue * SCREEN_WIDTH, 0);
    [_newsDetailCollectionView setContentOffset:point];
    
    [_newsDetailCollectionView setPagingEnabled:YES];
    _newsDetailCollectionView.backgroundColor = [UIColor whiteColor];
    _newsDetailCollectionView.delegate = self;
    _newsDetailCollectionView.dataSource = self;
    [self.view addSubview:_newsDetailCollectionView];
    
    [_newsDetailCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"newsDetailCollectionViewCell"];
}



#pragma mark --- 网络请求
- (void)newsDetailNetworkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        _newsDetailArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Newsesarray *newsesarray = [Newsesarray modelWithDic:dic];
            [_newsDetailArray addObject:newsesarray];
        }

        [self.newsDetailCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark ---collectionview返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newsDetailCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    Newsesarray *newsesarray = _newsDetailArray[indexPath.row];
    
    
    self.webView = [[CustomWebview alloc] initWithFrame:cell.bounds];
    NSString *link_share = [newsesarray link_share];
    [self.webView passStr:link_share];
    [cell addSubview:self.webView];
    
//    [cell addSubview:_bottomView];
    
    return cell;
}

#pragma mark ---collectionview的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
     return 10;
}


//- (void)shareButton
//{
//    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"57ddefde67e58ee7c8002fcb" shareText:@"你要分享的文字:" shareImage:[UIImage imageNamed:@"icon.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToEmail,nil] delegate:self];
//    NSLog(@"分享");
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
